<%--
  Created by IntelliJ IDEA.
  User: 14754
  Date: 2025/7/8
  Time: 21:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> <!-- 添加JSTL函数库 -->
<!DOCTYPE html>
<html>
<head>
    <title>场馆管理 - 智慧康养社区</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <script src="${pageContext.request.contextPath}/js/jquery/jquery-1.12.4.js"></script>
    <style>
        /* 复用原CSS基础样式 */
        .house-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 30px;
        }

        .house-header {
            margin-bottom: 30px;
        }

        .house-card {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.05);
            overflow: hidden;
            margin-bottom: 25px;
            transition: transform 0.3s, box-shadow 0.3s;
            height: 100%;
        }

        .house-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }

        .house-img {
            height: 120px;
            background-color: #e9f5eb;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #28a745;
            font-size: 3rem;
        }

        .house-body {
            padding: 20px;
        }

        .house-title {
            font-size: 1.2rem;
            font-weight: 600;
            margin-bottom: 10px;
        }

        .house-location {
            color: #6c757d;
            margin-bottom: 15px;
            font-size: 0.9rem;
        }

        .facility-tags {
            display: flex;
            flex-wrap: wrap;
            gap: 8px;
            margin: 10px 0;
        }

        .tag {
            background-color: #f1f8e9;
            color: #2e7d32;
            padding: 3px 10px;
            border-radius: 12px;
            font-size: 0.8rem;
        }

        .status-active {
            color: #28a745;
        }

        .status-inactive {
            color: #dc3545;
        }

        .detail-row {
            display: flex;
            justify-content: space-between;
            padding: 5px 0;
            border-bottom: 1px solid #f1f1f1;
        }

        .detail-label-bold {
            font-weight: 500;
            color: #444;
        }

        .filter-bar {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.05);
            padding: 20px;
            margin-bottom: 25px;
            position: relative;
            z-index: 10;
        }

        .filter-title {
            font-weight: 600;
            margin-bottom: 15px;
        }

        /* 编辑模态框样式 */
        .modal-content {
            border-radius: 12px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }

        .modal-header {
            border-bottom: none;
        }

        .modal-footer {
            border-top: none;
        }
        /* 删除按钮样式 */
        .btn-delete {
            background-color: #fff;
            color: #dc3545;
            border-color: #dc3545;
            transition: all 0.3s ease;
        }

        .btn-delete:hover {
            background-color: #dc3545;
            color: #fff;
            border-color: #dc3545;
        }

        .btn-delete:active {
            background-color: #bb2d3b !important;
            border-color: #bb2d3b !important;
        }
        .new-nav {
            width: 100%;
            background-color: #fff; /* 修改背景色为白色 */
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

        }

        .nav-container {
            display: flex;
            align-items: center;
            justify-content: space-between;
            max-width: 1200px;
            margin: 0 auto;
            padding: 0.5rem 1rem;
            height: 70px; /* 设置固定高度 */


        }

        .nav-brand a {
            color: #4CAF50; /* 绿色字体 */
            font-size: 1.4rem;
            font-weight: 700;
            text-decoration: none;
            font-family: "微软雅黑", sans-serif;
        }

        .nav-menu {
            display: flex;
            gap: 1.5rem;
            margin: 0;
            padding: 0;
            list-style: none;
            flex-grow: 1;
            justify-content: center; /* 场所管理居中 */
        }

        .nav-menu li a {
            color: #000; /* 黑色字体 */
            text-decoration: none;
            padding: 0.5rem 0;
            transition: color 0.3s ease;
            font-size: 1rem;
        }

        .nav-menu li a:hover,
        .nav-menu li a.active {
            color: #000;
        }

        .logout-btn {
            color: #000; /* 黑色字体 */
            text-decoration: none;
            padding: 0.5rem 1rem;
            border: 1px solid rgba(0, 0, 0, 0.3);
            border-radius: 4px;
            transition: all 0.3s ease;
            font-size: 0.95rem;
        }

        .logout-btn:hover {
            background-color: rgba(0, 0, 0, 0.1);
            border-color: #000;
            color: #87e8de;
        }

        .nav-menu .vibrant-community {
            color: #4CAF50; /* 绿色字体 */
            order: -1; /* 将活力长者社区放在左侧 */
        }

    </style>
</head>
<body>
<!-- 自定义导航栏 -->
<nav class="new-nav">
    <div class="nav-container">
        <div class="nav-brand">
            <a href="index.jsp">活力长者社区</a>
        </div>
        <div class="nav-user">
            <a href="login.jsp" class="logout-btn">退出登录</a>
        </div>
    </div>
</nav>
<!-- 添加场馆按钮 -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <button type="button" class="btn btn-success" onclick="openAddModal()">
        <i class="bi bi-plus-circle"></i> 添加新场馆
    </button>
</div>
<div class="house-container">
    <div class="house-header">
        <h2>场馆管理中心</h2>
        <p class="text-muted">为老年人提供休闲娱乐的场所信息管理</p>
    </div>

    <c:if test="${not empty error}">
        <div class="alert alert-danger">${error}</div>
    </c:if>

    <!-- 场馆筛选区 -->
    <div class="filter-bar">
        <div class="row">
            <div class="col-md-3">
                <div class="mb-3">
                    <label class="form-label filter-title">场馆类型</label>
                    <select id="venueType" class="form-select">
                        <option value="">不限</option>
                        <option value="文娱类" ${selectedVenueType == '文娱类' ? 'selected' : ''}>文娱类</option>
                        <option value="运动类" ${selectedVenueType == '运动类' ? 'selected' : ''}>运动类</option>
                        <option value="康养类" ${selectedVenueType == '康养类' ? 'selected' : ''}>康养类</option>
                        <option value="综合类" ${selectedVenueType == '综合类' ? 'selected' : ''}>综合类</option>
                    </select>
                </div>
            </div>
            <div class="col-md-3">
                <div class="mb-3">
                    <label class="form-label filter-title">使用状态</label>
                    <select id="usageStatus" class="form-select">
                        <option value="">不限</option>
                        <option value="1" ${selectedUsageStatus == '1' ? 'selected' : ''}>开放中</option>
                        <option value="0" ${selectedUsageStatus == '0' ? 'selected' : ''}>维护中</option>
                    </select>
                </div>
            </div>
            <div class="col-md-3">
                <div class="mb-3">
                    <label class="form-label filter-title">场馆位置</label>
                    <select id="location" class="form-select">
                        <option value="">不限</option>
                        <option value="阳光花园" ${selectedLocation == '阳光花园' ? 'selected' : ''}>阳光花园</option>
                        <option value="康养新区" ${selectedLocation == '康养新区' ? 'selected' : ''}>康养新区</option>
                        <option value="魔仙堡" ${selectedLocation == '魔仙堡' ? 'selected' : ''}>魔仙堡</option>
                    </select>
                </div>
            </div>
            <div class="col-md-3">
                <div class="mb-3">
                    <button class="btn btn-primary w-100 mt-4" onclick="filterFacilities()">筛选</button>
                    <button class="btn btn-outline-secondary w-100 mt-2" onclick="resetFilters()">重置</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 场馆列表展示区 -->
    <div class="row" id="venueList">
        <c:forEach items="${venueInfos}" var="venueInfo">
            <div class="col-md-4">
                <div class="house-card">
                    <div class="house-img">
                        <i class="bi bi-building"></i>
                    </div>
                    <div class="house-body">
                        <h3 class="house-title">${venueInfo.venueName}</h3>
                        <div class="house-location">
                            地址：${venueInfo.address} | 位置：${venueInfo.location}
                        </div>

                        <!-- 场馆标签 -->
                        <div class="facility-tags">
                            <span class="tag">${venueInfo.venueType}</span>
                            <span class="tag ${venueInfo.usageStatus == 1 ? 'status-active' : 'status-inactive'}">
                                    ${venueInfo.usageStatus == 1 ? '开放中' : '维护中'}
                            </span>
                            <span class="tag">
                  <!-- 修改后的判断逻辑 -->
                  ${fn:trim(venueInfo.chargeStandard.toString()) == '0' ? '免费' : '收费'}
              </span>
                        </div>

                        <!-- 详细信息行 -->
                        <div class="detail-row">
                            <span class="detail-label-bold">开放时间：</span>
                            <span>
    <fmt:formatDate value="${venueInfo.openingTime}" pattern="yyyy-MM-dd HH:mm:ss" /> -
    <fmt:formatDate value="${venueInfo.closingTime}" pattern="yyyy-MM-dd HH:mm:ss" />
  </span>
                        </div>
                        <div class="detail-row">
                            <span class="detail-label-bold">负责人：</span>
                            <span>${venueInfo.responsibleId}</span>
                        </div>
                        <div class="detail-row">
                            <span class="detail-label-bold">联系电话：</span>
                            <span>${venueInfo.contactPhone}</span>
                        </div>
                        <div class="detail-row">
                            <span class="detail-label-bold">场馆大小：</span>
                            <span>${venueInfo.size} ㎡</span>
                        </div>

                        <!-- 特别说明 -->
                        <div style="margin-top: 10px; font-size: 0.9rem; color: #666;">
                            <strong>特别说明：</strong>${venueInfo.specialNote}
                        </div>

                        <!-- 操作按钮 -->
                        <div style="margin-top: 15px; display: flex; gap: 10px;">
                            <button class="btn btn-sm btn-outline-success flex-1"
                                    onclick="openEditModal(${venueInfo.venueId}, '${venueInfo.venueName}', '${venueInfo.venueType}', ${venueInfo.usageStatus}, '${venueInfo.address}', '${venueInfo.location}', '${venueInfo.openingTime}', '${venueInfo.closingTime}', '${venueInfo.responsibleId}', '${venueInfo.contactPhone}', ${venueInfo.size}, '${venueInfo.specialNote}', ${venueInfo.chargeStandard})">编辑</button>
                            <a class="btn btn-sm btn-outline-danger flex-1"
                               href="VenueInfoListServlet?action=changeStatus&venueId=${venueInfo.venueId}"
                               onclick="return confirm('确定要更改该场馆的使用状态吗？')">切换状态</a>
                            <button class="btn btn-sm btn-delete flex-1"
                                    onclick="confirmDelete(${venueInfo.venueId}, '${venueInfo.venueName}')">
                                <i class="bi bi-trash"></i> 删除
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </c:forEach>
    </div>

    <!-- 无数据提示 - 仅当用户至少选择了一个筛选条件且无结果时显示 -->
    <c:if test="${empty venueInfos && (param.venueType != null || param.usageStatus != null || param.location != null)}">
        <div class="alert alert-info text-center" id="emptyResult">
            <i class="bi bi-info-circle"></i> 暂无符合条件的场馆信息
        </div>
    </c:if>
</div>

<!-- 编辑模态框 -->
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="editModalLabel">编辑场馆信息</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="editForm">
                    <input type="hidden" id="editVenueId" name="venueId">

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">场馆名称</label>
                            <input type="text" class="form-control" id="editVenueName" name="venueName" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">场馆类型</label>
                            <select class="form-select" id="editVenueType" name="venueType" required>
                                <option value="文娱类">文娱类</option>
                                <option value="运动类">运动类</option>
                                <option value="康养类">康养类</option>
                                <option value="综合类">综合类</option>
                            </select>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">使用状态</label>
                            <select class="form-select" id="editUsageStatus" name="usageStatus" required>
                                <option value="1">开放中</option>
                                <option value="0">维护中</option>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">收费标准</label>
                            <select class="form-select" id="editChargeStandard" name="chargeStandard" required>
                                <option value="0">免费</option>
                                <option value="1">收费</option>
                            </select>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">场馆地址</label>
                            <input type="text" class="form-control" id="editAddress" name="address" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">场馆位置</label>
                            <select class="form-select" id="editLocation" name="location" required>
                                <option value="阳光花园">阳光花园</option>
                                <option value="康养新区">康养新区</option>
                                <option value="魔仙堡">魔仙堡</option>
                            </select>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">开放时间</label>
                            <input type="time" class="form-control" id="editOpeningTime" name="openingTime"  step="1" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">关闭时间</label>
                            <input type="time" class="form-control" id="editClosingTime" name="closingTime" step="1" required>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">负责人</label>
                            <input type="text" class="form-control" id="editResponsibleId" name="responsibleId" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">联系电话</label>
                            <input type="text" class="form-control" id="editContactPhone" name="contactPhone" required>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">场馆大小 (㎡)</label>
                            <input type="number" class="form-control" id="editSize" name="size" min="1" required>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">特别说明</label>
                        <textarea class="form-control" id="editSpecialNote" name="specialNote" rows="3"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="saveVenue()">保存</button>
            </div>
        </div>
    </div>
</div>
<!-- 新增场馆模态框 -->
<div class="modal fade" id="addModal" tabindex="-1" aria-labelledby="addModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addModalLabel">添加新场馆</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="addForm">
                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">场馆名称 <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="addVenueName" name="venueName" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">场馆类型 <span class="text-danger">*</span></label>
                            <select class="form-select" id="addVenueType" name="venueType" required>
                                <option value="">请选择类型</option>
                                <option value="文娱类">文娱类</option>
                                <option value="运动类">运动类</option>
                                <option value="康养类">康养类</option>
                                <option value="综合类">综合类</option>
                            </select>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">使用状态 <span class="text-danger">*</span></label>
                            <select class="form-select" id="addUsageStatus" name="usageStatus" required>
                                <option value="1">开放中</option>
                                <option value="0">维护中</option>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">收费标准 <span class="text-danger">*</span></label>
                            <select class="form-select" id="addChargeStandard" name="chargeStandard" required>
                                <option value="0">免费</option>
                                <option value="1">收费</option>
                            </select>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">场馆地址 <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="addAddress" name="address" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">场馆位置 <span class="text-danger">*</span></label>
                            <select class="form-select" id="addLocation" name="location" required>
                                <option value="">请选择位置</option>
                                <option value="阳光花园">阳光花园</option>
                                <option value="康养新区">康养新区</option>
                                <option value="魔仙堡">魔仙堡</option>
                            </select>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">开放时间 <span class="text-danger">*</span></label>
                            <input type="time" class="form-control" id="addOpeningTime" name="openingTime" step="1" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">关闭时间 <span class="text-danger">*</span></label>
                            <input type="time" class="form-control" id="addClosingTime" name="closingTime" step="1" required>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">负责人 <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="addResponsibleId" name="responsibleId" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">联系电话 <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="addContactPhone" name="contactPhone" required>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-md-6">
                            <label class="form-label">场馆大小 (㎡) <span class="text-danger">*</span></label>
                            <input type="number" class="form-control" id="addSize" name="size" min="1" required>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">特别说明</label>
                        <textarea class="form-control" id="addSpecialNote" name="specialNote" rows="3"></textarea>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="addNewVenue()">保存</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteModalLabel">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>确定要删除场馆 <span id="deleteVenueName" class="fw-bold"></span> 吗？此操作不可撤销！</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger" id="confirmDeleteBtn">确认删除</button>
            </div>
        </div>
    </div>
</div>
<c:import url="footer.jsp" />
<script>
    // 筛选场馆
    function filterFacilities() {
        const venueType = document.getElementById('venueType').value;
        const usageStatus = document.getElementById('usageStatus').value;
        const location = document.getElementById('location').value;

        const form = document.createElement('form');
        form.method = 'POST';
        form.action = 'VenueInfoListServlet';

        // 总是添加所有参数，值为空字符串表示不筛选
        form.appendChild(createInput('venueType', venueType));
        form.appendChild(createInput('usageStatus', usageStatus));
        form.appendChild(createInput('location', location));
        // 指定action为list，确保调用listVenues方法
        form.appendChild(createInput('action', 'list'));

        document.body.appendChild(form);
        form.submit();
    }

    // 重置筛选条件
    function resetFilters() {
        document.getElementById('venueType').value = '';
        document.getElementById('usageStatus').value = '';
        document.getElementById('location').value = '';
        // 提交空参数，显示全部
        document.location.href = 'VenueInfoListServlet';
    }

    // 创建隐藏表单字段
    function createInput(name, value) {
        const input = document.createElement('input');
        input.type = 'hidden';
        input.name = name;
        input.value = value || '';
        return input;
    }

    let currentDeleteId = null;

    function confirmDelete(venueId, venueName) {
        currentDeleteId = venueId;
        document.getElementById('deleteVenueName').textContent = venueName;
        const deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
        deleteModal.show();
    }

    document.getElementById('confirmDeleteBtn').addEventListener('click', function() {
        if (currentDeleteId) {
            $.ajax({
                url: 'VenueInfoListServlet',
                method: 'POST',
                data: {
                    action: 'delete',
                    venueId: currentDeleteId
                },
                success: function(response) {
                    const deleteModal = bootstrap.Modal.getInstance(document.getElementById('deleteModal'));
                    deleteModal.hide();
                    showNotification('场馆已成功删除', 'success');
                    setTimeout(() => location.reload(), 1000);
                },
                error: function(xhr) {
                    showNotification('删除失败: ' + xhr.responseText, 'danger');
                }
            });
        }
    });
    // 打开编辑模态框
    function openEditModal(venueId, venueName, venueType, usageStatus, address, location, openingTime, closingTime, responsibleId, contactPhone, size, specialNote, chargeStandard) {
        console.log('打开编辑模态框，场馆ID:', venueId);
        console.log('原始时间格式 - 开始:', openingTime, '结束:', closingTime); // 新增日志

        // 填充表单数据（重点修复时间和负责人ID）
        document.getElementById('editVenueId').value = venueId;
        document.getElementById('editVenueName').value = venueName;
        document.getElementById('editVenueType').value = venueType;
        document.getElementById('editUsageStatus').value = usageStatus;
        document.getElementById('editAddress').value = address;
        document.getElementById('editLocation').value = location;

        // 修复时间格式：确保为HH:MM（兼容Time.valueOf()）
        const formattedOpenTime = formatTime(openingTime);
        const formattedCloseTime = formatTime(closingTime);
        document.getElementById('editOpeningTime').value = formattedOpenTime;
        document.getElementById('editClosingTime').value = formattedCloseTime;
        console.log('格式化后时间 - 开始:', formattedOpenTime, '结束:', formattedCloseTime); // 新增日志

        // 负责人ID可能是字符串，直接赋值（不强制转换）
        document.getElementById('editResponsibleId').value = responsibleId;
        document.getElementById('editContactPhone').value = contactPhone;
        document.getElementById('editSize').value = size;
        document.getElementById('editSpecialNote').value = specialNote;
        document.getElementById('editChargeStandard').value = chargeStandard;

        // 显示模态框
        const editModal = new bootstrap.Modal(document.getElementById('editModal'));
        editModal.show();
    }
    // 增强时间格式化函数（确保兼容Time.valueOf()）
    function formatTime(timeString) {
        if (!timeString) return '';
        // 处理可能的格式：HH:MM:SS、HH:MM、java.sql.Time的字符串形式
        const timeParts = timeString.toString().split(':'); // 转为字符串再分割
        if (timeParts.length >= 2) {
            return `${timeParts[0].padStart(2, '0')}:${timeParts[1].padStart(2, '0')}`; // 确保两位数
        }
        console.error('时间格式错误:', timeString);
        return ''; // 空值由前端表单验证处理
    }
    // 保存场馆信息
    function saveVenue() {
        const formData = new FormData(document.getElementById('editForm'));
        formData.append('action', 'update'); // 传递 update 动作
        const venueId = formData.get('venueId');
        console.log('提交编辑表单，场馆ID:', venueId);
        $.ajax('VenueInfoListServlet',{
            method:"post",
            data:Object.fromEntries(formData),
            success(data){
                console.log(data)
                console.log('保存成功:', data);
                window.location =   window.location
                const editModal = bootstrap.Modal.getInstance(document.getElementById('editModal'));
                editModal.hide();
                showNotification('场馆信息已成功更新', 'success');
                window.location =   window.location
                setTimeout(() => location.reload(), 1500);
            },
            error(e){
                throw new Error(`更新失败了`);
            }
        })
    }

    function formatTime(timeString) {
        if (!timeString) return '';

        // 处理包含日期的格式（如yyyy-MM-dd HH:mm:ss或yyyy-MM-ddTHH:mm:ss）
        if (timeString.includes(' ') || timeString.includes('T')) {
            const timePart = timeString.includes(' ') ? timeString.split(' ')[1] : timeString.split('T')[1];
            // 确保格式为HH:mm:ss，若只有HH:mm则补全秒
            return timePart.length >= 8 ? timePart.slice(0, 8) : `${timePart}:00`;
        }

        // 处理已有时分秒的情况
        if (timeString.includes(':')) {
            const parts = timeString.split(':');
            if (parts.length === 2) {
                // 只有时分，补全秒
                return `${parts[0].padStart(2, '0')}:${parts[1].padStart(2, '0')}:00`;
            } else if (parts.length === 3) {
                // 已有时分秒，格式化确保两位数字
                return `${parts[0].padStart(2, '0')}:${parts[1].padStart(2, '0')}:${parts[2].padStart(2, '0')}`;
            }
        }

        console.error('时间格式错误:', timeString);
        return '';
    }
    // 打开新增场馆模态框
    function openAddModal() {
        // 重置表单
        document.getElementById('addForm').reset();
        // 显示模态框
        const addModal = new bootstrap.Modal(document.getElementById('addModal'));
        addModal.show();
    }

    // 添加新场馆
    function addNewVenue() {
        const formData = new FormData(document.getElementById('addForm'));
        formData.append('action', 'add'); // 传递 add 动作

        // 前端简单验证
        if (!formData.get('venueName')) {
            alert('请输入场馆名称');
            return;
        }

        if (!formData.get('venueType')) {
            alert('请选择场馆类型');
            return;
        }

        $.ajax('VenueInfoListServlet', {
            method: "post",
            data: Object.fromEntries(formData),
            success: function(data) {
                console.log('新增成功:', data);
                // 隐藏模态框
                const addModal = bootstrap.Modal.getInstance(document.getElementById('addModal'));
                addModal.hide();
                // 显示成功提示并刷新页面
                alert('场馆添加成功！');
                location.reload();
            },
            error: function(e) {
                console.error('新增失败:', e);
                alert('添加失败，请稍后重试');
            }
        });
    }

    function confirmLogout() {
        if (confirm('确定要退出登录吗？')) {
            window.location.href = 'login.jsp'; // 替换为实际的退出登录URL
        }
    }
    // 显示通知函数（可以用于替代alert，提供更友好的提示）
    function showNotification(message, type) {
        // 创建通知元素
        const notification = document.createElement('div');
        notification.className = `alert alert-${type} fixed-top m-3 z-3`;
        notification.role = 'alert';
        notification.textContent = message;

        // 添加到页面
        document.body.appendChild(notification);

        // 3秒后自动消失
        setTimeout(() => {
            notification.classList.add('fade');
            setTimeout(() => notification.remove(), 500);
        }, 3000);
    }
</script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>